package models

import (
	"fmt"
	"regexp"
	"time"

	"github.com/go-playground/validator/v10"
)

var mobileRegex = regexp.MustCompile(`^1[3-9]\d{9}$`)

func VMobile(fl validator.FieldLevel) bool {
	return mobileRegex.MatchString(fl.Field().String())
}

func VTime(fl validator.FieldLevel) bool {
	value := fl.Field().String()
	_, err := time.Parse(time.DateTime, value)
	if err != nil {
		println(err)
		return false
	}
	return true
}

func VDateOnly(fl validator.FieldLevel) bool {
	value := fl.Field().String()
	if value == "" {
		return true
	}
	_, err := time.Parse(time.DateOnly, value)
	if err != nil {
		return false
	}
	return true
}

func VCnGte(fl validator.FieldLevel) bool {
	value := fl.Field().String()
	min := 0
	// 从 tag 获取参数，例如 `chinese_len=10`
	param := fl.Param()
	_, err := fmt.Sscanf(param, "%d", &min)
	if err != nil {
		return false // 参数解析失败，验证失败
	}

	// 按 rune（Unicode 字符）统计长度
	return len([]rune(value)) >= min
}
func VCnLte(fl validator.FieldLevel) bool {
	value := fl.Field().String()
	max := 0

	// 从 tag 获取参数，例如 `chinese_len=10`
	param := fl.Param()
	_, err := fmt.Sscanf(param, "%d", &max)
	if err != nil {
		return false // 参数解析失败，验证失败
	}

	// 按 rune（Unicode 字符）统计长度
	return len([]rune(value)) <= max
}
